package com.moli.iov.base.remote.impl;

import com.moli.iov.base.enums.AlarmAuthEnum;
import com.moli.iov.base.enums.AuthorizedEnum;
import com.moli.iov.base.model.dto.req.VehicleBaseAuthReqDto;
import com.moli.iov.base.model.dto.req.VehicleBaseReqDto;
import com.moli.iov.base.model.dto.res.AuthDto;
import com.moli.iov.base.model.dto.res.LoginGateResDto;
import com.moli.iov.base.model.dto.res.TboxBindingDto;
import com.moli.iov.base.model.dto.res.VehicleBaseAuthResDto;
import com.moli.iov.base.model.po.VehicleBase;
import com.moli.iov.base.model.po.VehicleBaseAuth;
import com.moli.iov.base.model.po.VehicleTbox;
import com.moli.iov.base.remote.IVehicleBaseAuthRemoteService;
import com.moli.iov.base.service.IVehicleBaseAuthService;
import com.moli.iov.base.service.IVehicleBaseService;
import com.moli.iov.base.service.IVehicleTboxService;
import com.moli.iov.rest.RestResponse;
import com.moli.iov.util.CommonUtils;
import com.moli.iov.util.StringUtil;
import com.moli.iov.util.ValidationUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 车辆基础授权状态表 服务实现类
 * </p>
 *
 * @author wanghualin
 * @date 2020 -02-26 14:44:36
 */
@Service(group = "${service.group}")
@Slf4j
@Transactional(rollbackFor = Exception.class)
public class IVehicleBaseAuthRemoteServiceImpl implements IVehicleBaseAuthRemoteService {
    @Autowired
    private IVehicleBaseAuthService baseAuthService;
    @Autowired
    private IVehicleBaseService iVehicleBaseService;
    @Autowired
    private IVehicleTboxService iVehicleTboxService;

    @Override
    public RestResponse<AuthDto> getVehicleBaseAuthByVin(String vin) {
        if (StringUtil.isBlank(vin)){
            return RestResponse.failure("车架号不能为空");
        }
        VehicleBase base = iVehicleBaseService.getVehicleBaseByVin(vin);
        if (base==null){
            return RestResponse.failure("车辆不存在");
        }
        VehicleBaseAuth auth=baseAuthService.getVehicleBaseAuthByVin(vin);
         if (auth==null){
             return RestResponse.failure("车辆授权不存在");
         }
        AuthDto authDto = AuthDto.newInstance();
        List<Integer> alarmAuths=new ArrayList<>();
        if (auth.getFenceAlarmAuthStatus().intValue()== AuthorizedEnum.UN_AUTHORIZED.getCode()){
            alarmAuths.add(AlarmAuthEnum.FENCE.getFlag());
         }
        if (auth.getLostAlarmAuthStatus().intValue()== AuthorizedEnum.UN_AUTHORIZED.getCode()){
            alarmAuths.add(AlarmAuthEnum.OFF_LINE.getFlag());
        }
        if (auth.getMileageAlarmAuthStatus().intValue()== AuthorizedEnum.UN_AUTHORIZED.getCode()){
            alarmAuths.add(AlarmAuthEnum.MILE_EXCHANGE.getFlag());
        }
        if (auth.getTerminalAuthStatus().intValue()== AuthorizedEnum.UN_AUTHORIZED.getCode()){
            alarmAuths.add(AlarmAuthEnum.REAL.getFlag());
        }
        if (auth.getLoginAuthStatus().intValue()== AuthorizedEnum.UN_AUTHORIZED.getCode()){
            alarmAuths.add(AlarmAuthEnum.Login.getFlag());
        }
        authDto.setAlarmAuths(alarmAuths);
        return RestResponse.success(authDto);
    }

    @Override
    public RestResponse<LoginGateResDto> getVehicleBaseAuthByVin(String vin, String iccid) {
        VehicleTbox tbox=iVehicleTboxService.getVehicleTboxByIccid(iccid);
         if (tbox==null){
             return RestResponse.failure("终端不存在");
         }
        VehicleBase vehicleBase = iVehicleBaseService.getVehicleBaseByVin(vin);
         if (vehicleBase==null){
             return    RestResponse.failure("车辆不存在");
         }
        VehicleBaseAuth vehicleBaseAuth = baseAuthService.getVehicleBaseAuthByVin(vin);
        if (vehicleBase==null){
            return    RestResponse.failure("授权不存在");
        }
        LoginGateResDto loginGateResDto=new LoginGateResDto();

        loginGateResDto.setDeviceId(tbox.getDeviceId());
        VehicleTbox binding = iVehicleTboxService.getVehicleTboxByVin(vin);
        if (binding!=null){
            TboxBindingDto tboxBindingDto=new TboxBindingDto();
            CommonUtils.copyProperties(binding,tboxBindingDto);
            loginGateResDto.setBindingDto(tboxBindingDto);
         }
         if(vehicleBase.getAuthorizedStatus().longValue()==AuthorizedEnum.AUTHORIZED.getCode()){
             loginGateResDto.setVehicleBaseAuth(true);
         }
        if(tbox.getAuthorizedStatus().longValue()==AuthorizedEnum.AUTHORIZED.getCode()){
            loginGateResDto.setVehicleTboxAuth(true);
        }
        if(vehicleBaseAuth.getLoginAuthStatus().longValue()==AuthorizedEnum.AUTHORIZED.getCode()){
            loginGateResDto.setVehicleBaseLoginAuth(true);
        }
        return RestResponse.success(loginGateResDto);
    }

    @Override
    public RestResponse updateVehicleBaseAuth(VehicleBaseAuthReqDto dto) {
        ValidationUtils.validateBean(dto);
        log.info("修改车联授权状态入参：{}", dto);
        VehicleBase vehicleBase = iVehicleBaseService.getVehicleBaseByVin(dto.getVin());
        Date date = Calendar.getInstance().getTime();
        if (vehicleBase == null) {
            return RestResponse.failure("车辆不存在");
        }
        if (dto.getVehicleAuhStatus() != null) {
            VehicleBaseReqDto vehicleBaseReqDto = new VehicleBaseReqDto();
            CommonUtils.copyProperties(vehicleBase, vehicleBaseReqDto);
            vehicleBaseReqDto.setModifiedUser(dto.getSysUserId().toString());
            vehicleBaseReqDto.setGmtModified(date);
            vehicleBaseReqDto.setAuthorizedStatus(dto.getVehicleAuhStatus());
            // 修改 车辆授权为未授权
            if (dto.getVehicleAuhStatus().intValue() == AuthorizedEnum.UN_AUTHORIZED.getCode()) {
                iVehicleBaseService.updateVehicleBase(vehicleBaseReqDto);
                VehicleBaseAuth vehicleBaseAuth = baseAuthService.getVehicleBaseAuthByVin(dto.getVin());
                setBaseAuthUnAuthorized(vehicleBaseAuth);
                vehicleBaseAuth.setModifiedUser(dto.getSysUserId().toString());
                vehicleBaseAuth.setGmtModified(date);
                return baseAuthService.updateVehicleBaseAuth(vehicleBaseAuth);
            } else {
                return iVehicleBaseService.updateVehicleBase(vehicleBaseReqDto);
            }
        } else {
            VehicleBaseAuth vehicleBaseAuth = baseAuthService.getVehicleBaseAuthByVin(dto.getVin());
            if (vehicleBase == null) {
                return RestResponse.failure("车辆授权存在");
            }
            CommonUtils.copyProperties(dto, vehicleBaseAuth);
            vehicleBaseAuth.setModifiedUser(dto.getSysUserId().toString());
            vehicleBaseAuth.setGmtModified(date);
            return baseAuthService.updateVehicleBaseAuth(vehicleBaseAuth);
        }
    }

    /**
     * 全部设置为未授权 排除登录授权
     *
     * @param vehicleBaseAuth
     */
    private void setBaseAuthUnAuthorized(VehicleBaseAuth vehicleBaseAuth) {
        vehicleBaseAuth.setLostAlarmAuthStatus(AuthorizedEnum.UN_AUTHORIZED.getCode());
        vehicleBaseAuth.setFenceAlarmAuthStatus(AuthorizedEnum.UN_AUTHORIZED.getCode());
        vehicleBaseAuth.setMileageAlarmAuthStatus(AuthorizedEnum.UN_AUTHORIZED.getCode());
        vehicleBaseAuth.setTerminalAuthStatus(AuthorizedEnum.UN_AUTHORIZED.getCode());
    }

    @Override
    public RestResponse<VehicleBaseAuthResDto> getVehicleBaseAuth(VehicleBaseAuthReqDto dto){
        VehicleBaseAuthResDto resDto = new VehicleBaseAuthResDto();

        VehicleBaseAuth vehicleBaseAuth = baseAuthService.getVehicleBaseAuthByVin(dto.getVin());
        if(vehicleBaseAuth!=null){
            CommonUtils.copyProperties(vehicleBaseAuth, resDto);
        }

        return RestResponse.success(resDto);
    }
}
